package gupao.concurrency.simpledemo.risk.service;

import gupao.concurrency.simpledemo.risk.model.NodeTree;
import gupao.concurrency.simpledemo.risk.model.RiskCapitalCalcContext;
import gupao.concurrency.simpledemo.risk.model.Node;
import gupao.concurrency.simpledemo.risk.simulator.CalcSimulator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;

public class SimpleCalculateServiceImpl implements CalculateService{

    private static final Log logger = LogFactory.getLog(SimpleCalculateServiceImpl.class);

    @Override
    public void calculateRiskCapital(RiskCapitalCalcContext context) {
        NodeTree tree = context.getDeptTree();
        long start = System.currentTimeMillis();
        for(int i = tree.getMinLevel(); i <= tree.getMaxLevel(); i++){
            List<Node> levelNodeList = tree.getLevelMap().get(i);
            String level = "level" + i;
            logger.info("Start to calculate " + level + " nodes: total number = " + levelNodeList.size());
            long start2 = System.currentTimeMillis();
            for (Node node : levelNodeList) {
                CalcSimulator.calcNodeQaf(node, tree);
                CalcSimulator.calcRiskCaptital(node, tree);
            }
            logger.info("Done. time cost:" + (System.currentTimeMillis()-start2) + "ms");
        }
        logger.info("Completed. Total time cost:" + (System.currentTimeMillis()-start) + "ms");
    }
}
